package org.grill.fatwhacker.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.grill.fatwhacker.data.DailyRecord;
import org.grill.fatwhacker.data.FoodWeek;
import org.joda.time.LocalDate;

public class WeekDetailView {

	DayMiniView[] views = new DayMiniView[7];
	
	Label flexLabel;
	
	Text dailyPointsText;
	
	public WeekDetailView(Composite weekViewComposite) {
		GridLayout weekViewLayout = new GridLayout();
		
		Composite topComposite = new Composite(weekViewComposite, SWT.NONE);
		GridLayout topLayout = new GridLayout();
		topLayout.marginHeight = -2;
		topLayout.numColumns = 2;
		topLayout.horizontalSpacing = 5;
		topLayout.verticalSpacing = 5;
		topComposite.setLayout(topLayout);
		
		new Label(topComposite, SWT.NONE).setText("Points/Day: ");
		dailyPointsText = new Text(topComposite, SWT.BORDER);
		GridData pointData = new GridData();
		pointData.widthHint = 50;
		dailyPointsText.setLayoutData(pointData);
		dailyPointsText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				try {
					int num = Integer.parseInt(dailyPointsText.getText());
					currentWeek.setDailyPoints(num);
					render(false);
				} catch (NumberFormatException e) {}
			}
		});

		flexLabel = new Label(topComposite, SWT.NONE);
		flexLabel.setText("Flexpoints:                ");
		GridData flexData = new GridData(GridData.FILL_HORIZONTAL);
		flexData.horizontalSpan = 2;
		flexLabel.setLayoutData(flexData);
		Utils.setBoldFont(flexLabel);
		flexLabel.setForeground(flexLabel.getDisplay().getSystemColor(SWT.COLOR_BLUE));
		

		
		weekViewLayout.verticalSpacing = 10;
		
		for (int i = 0; i < 7; i++) {
			views[i] = new DayMiniView(weekViewComposite);
			final int i1 = i;
			views[i].addSelectionListener(new Listener() {
				public void handleEvent(Event arg0) {
					selectionChanged(views[i1]);
				}
			});
		}

		weekViewComposite.setLayout(weekViewLayout);
	}
	
	
	private void selectionChanged(DayMiniView selected) {
		for (DayMiniView view : views) {
			if (view != selected) {
				view.setSelected(false);
			}
		}
		
		dayDetailView.setDay(selected.getRecord());
		dayDetailView.render();
		
		render();
	}

	public void setWeek(FoodWeek foodWeek) {
		currentWeek = foodWeek;
	}
	
	private FoodWeek currentWeek;
	
	public void render() {
		render(true);
	}

	public void render(boolean renderInput) {
		flexLabel.setText("Flexpoints Left: " + (currentWeek.getFlexPoints() - currentWeek.getFlexpointsUsed()));
		if (renderInput)
			dailyPointsText.setText(String.valueOf(currentWeek.getDailyPoints()));
		
		
		boolean anySelected = false;
		int selectedIndex = -1;
		for (int i = 0; i < 7; i++) {
			DailyRecord record = currentWeek.getDays().get(i);
			views[i].setRecord(record);
			if (views[i].isSelected()) {
				anySelected = true;
				selectedIndex = i;
			}
		}
		
		if (!anySelected) {
			int i = 0;
			for (DayMiniView view : views) {
				if (view.getRecord().getDate().equals(new LocalDate())) {
					anySelected = true;
					view.setSelected(true);
					selectedIndex = i; 
					break;
				}
				i++;
			}
		}
		
		if (!anySelected) {
			selectedIndex = 0;
			views[0].setSelected(true);
		}
		
		int i = 0;
		for (DayMiniView view : views) {
			if (selectedIndex != i++)
				view.setSelected(false);
			view.render();
		}
	}
	
	private DayDetailView dayDetailView;

	public void setDayDetailView(DayDetailView dayDetailView) {
		this.dayDetailView = dayDetailView;
	}
}
